﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace EnerForecast
{
    class Dry2Wet
    {
        private DataSet drywetSet; 
        public Dry2Wet()
        {
            string excelpath = "干湿球温度、湿度换算表.xls";
            //确定excel版本
            string strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + excelpath + ";Extended Properties=\"Excel 12.0;\"";
            if ((System.IO.Path.GetExtension(excelpath)).ToLower() == ".xls")
            {
                strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + excelpath + ";Extended Properties=\"Excel 8.0;IMEX=1\"";
            }
            using (OleDbConnection conn = new OleDbConnection(strconn))
            {
                //打开oledb连接
                conn.Open();
                //取sheet1的数据
                string strCom = "SELECT * FROM [Sheet1$]";
                OleDbDataAdapter myadapter = new OleDbDataAdapter(strCom, conn);
                drywetSet = new DataSet();
                myadapter.Fill(drywetSet);
            }
        }
        public double Convert2wet(int idrytemp,double humidity){
            double wettemp;
            DataTable temptable =  drywetSet.Tables[0];
            int ihumidity = int.Parse(humidity.ToString()) / 5 - 3;
            if (idrytemp < 0)
            {
                wettemp = idrytemp - 0.25 * (17 - ihumidity);
            }
            else {
                try
                {
                    wettemp = double.Parse(temptable.Rows[idrytemp][ihumidity].ToString());
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return wettemp;
        }
    }
}
